package cn.mulanbay.auth.service;

import cn.mulanbay.api.system.service.RemoteUserService;
import cn.mulanbay.business.bean.UserLoginInfo;
import cn.mulanbay.common.config.Constants;
import cn.mulanbay.common.domain.R;
import cn.mulanbay.common.exception.ApplicationException;
import cn.mulanbay.persistent.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author fenghong
 * @create 2022-01-16 10:00
 */
@Component
public class AuthService {

    @Autowired
    RemoteUserService remoteUserService;

    /**
     * 获取用户
     * @param username
     * @return
     */
    public User getUser(String username){
        R<User> r = remoteUserService.getUserByUsername(username);
        if(r.getCode()!= Constants.SUCCESS_CODE){
            throw new ApplicationException(r.getCode(),r.getMessage());
        }else{
            return r.getData();
        }
    }

    /**
     * 用户用户角色编号
     * @param userId
     * @return
     */
    public Long getUserRoleId(Long userId){
        R<Long> r = remoteUserService.getRoleId(userId);
        if(r.getCode()!= Constants.SUCCESS_CODE){
            throw new ApplicationException(r.getCode(),r.getMessage());
        }else{
            return r.getData();
        }
    }

    /**
     * 更新用户登录信息
     * @param uli
     * @return
     */
    public Boolean updateLoginInfo(UserLoginInfo uli){
        return remoteUserService.updateLoginInfo(uli).getData();
    }
}
